package edu.cmu.cs.webapp.ioms.ui.dialogs.order;


import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.cmu.cs.webapp.ioms.models.OrderStatus;
import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

public class OrderStatusDialog extends TitleAreaDialog {

	private Text _orderID;
	private Combo _oldStatus;
	private Combo _newStatus;
	private PurchaseOrder _model;

	public OrderStatusDialog(Shell parentShell, PurchaseOrder model) {
		super(parentShell);
		this._model = model;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		setTitle("Accept Order");
		setMessage("Review the order details");

		Composite container = SWTUtils.createComposite((Composite) super
				.createDialogArea(parent), 2, SWT.DEFAULT, 1);
		SWTUtils.createLabel(container, "Order ID:", 1);
		
		_orderID = SWTUtils.createTextBox(container);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		_orderID.setLayoutData(gd);
		_orderID.setEditable(false);

		SWTUtils.createSeparator(container, 2);

		SWTUtils.createLabel(container, "Original Status:", 1);
		_oldStatus = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		_oldStatus.setItems(OrderStatus.getLiterals());
		_oldStatus.setLayoutData(gd);
		_oldStatus.setEnabled(false);

		SWTUtils.createLabel(container, "New Status:", 1);
		_newStatus = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		_newStatus.setLayoutData(gd);
		_newStatus.setItems(new String[] { "Accepted", "In Process",
				"Delivered" });

		loadFromExistingModel();

		return container;
	}

	private void loadFromExistingModel() {
		if (this._model != null) {
			this._orderID.setText("" + this._model.getOrder_id());
			this._oldStatus.setText(this._model.getStatus().getLiteral());
		}
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (_oldStatus.getText().equals(OrderStatus.PENDING.getLiteral())
				&& !_newStatus.getText().equals(
						OrderStatus.PENDING.getLiteral())) {
			// update inventory stock level;

		}

		_model.setStatus(OrderStatus.get(getNewStatus()));

		super.buttonPressed(buttonId);
	}

	private String getNewStatus() {
		return this._newStatus.getText();

	}

	public PurchaseOrder getModel() {
		return _model;
	}

}
